/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.thingsgrid.notice.controller;

import com.thingsgrid.common.cache.CacheNames;
import com.thingsgrid.notice.entity.NoticeEntiy;
import com.thingsgrid.notice.entity.SendEntiy;
import com.thingsgrid.notice.exception.NoticeException;
import com.thingsgrid.notice.feign.INoticeClient;
import com.thingsgrid.notice.service.INoticeService;
import com.thingsgrid.notice.service.ISendService;
import com.thingsgrid.notice.service.InsService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

/**
 * 控制器
 * @author MQ
 */
@RestController
@RequestMapping("notice")
@AllArgsConstructor
@Api(value = "消息通知", tags = "消息通知接口")
public class NoticeController extends BladeController implements CacheNames {
	private INoticeService<NoticeEntiy> iNoticeService;
	private ISendService<SendEntiy> iSendService;
	private InsService<NoticeEntiy,SendEntiy> insService;
	private INoticeClient iNoticeClient;
	/**
	 * 详情
	 */
	@GetMapping("/v1/message/content")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取单个站内信内容", notes = "传入notice ID")
	public R<NoticeEntiy> detail(@RequestParam("msgId") String msgId) {
		try{
			return iNoticeService.findOneById(msgId);
		}catch (NoticeException e){
			return R.fail(e.getR().getMsg());
		}

	}

	/**
	 * 分页
	 */
	@PostMapping("/v1/message/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "content", value = "消息内容", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "title", value = "消息标题", paramType = "query", dataType = "string")
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取站内信列表", notes = "传入notice")
	public R<Page<NoticeEntiy>> list(@ApiIgnore  @RequestBody NoticeEntiy noticeEntiy, Pageable pageable) {
		try{
			BladeUser user = SecureUtil.getUser();
			noticeEntiy.setCreatorName(user.getAccount());
			Page<NoticeEntiy> pages = iNoticeService.page(noticeEntiy,pageable);
			return R.data(pages);
		}catch (NoticeException e){
			return R.fail(e.getR().getMsg());
		}
	}


	/**
	 * 发送消息
	 */
	@PostMapping("/v1/message")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "发送消息", notes = "传入notice")
	public R save(@RequestBody NoticeEntiy noticeEntiy,@RequestBody SendEntiy sendEntiy) {
		return iNoticeClient.sendMsg(noticeEntiy.getMessageType(),noticeEntiy.getSendType(),noticeEntiy.getTitle(),noticeEntiy.getContent(),sendEntiy.getReceiverName());
	}
	/**
	 * 批量删除
	 */
	@PutMapping("/v1/delete_message")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "批量删除", notes = "传入ids")
	public R remove(@ApiParam(value = "ID集合") @RequestParam("messageIds") String ids) {
		try{
			return insService.deleteByIds(ids);
		}catch (NoticeException e){
			return R.fail(e.getR().getMsg());
		}
	}

	/**
	 * 全部删除
	 */
	@PutMapping("/v1/delete_all_message")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "全部删除", notes = "传入notice")
	public R removeAll() {
		try{
			BladeUser user = SecureUtil.getUser();
			return insService.deleteAll(user.getAccount());
		}catch (NoticeException e){
			return R.fail(e.getR().getMsg());
		}
	}

	/**
	 * 批量已阅
	 */
	@PutMapping("/v1/read_message")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "批量已阅", notes = "传入ids")
	public R readBatch(@ApiParam(value = "ID集合") @RequestParam String messageIds) {
		try{
			return iSendService.readBatch(messageIds);
		}catch (NoticeException e){
			return R.fail(e.getR().getMsg());
		}
	}
	/**
	 * 全部已阅
	 */
	@PutMapping("/v1/read_all_message")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "全部已阅", notes = "传入ids")
	public R readAll() {
		try{
			BladeUser user = SecureUtil.getUser();
			return iSendService.readAll(user.getAccount());
		}catch (NoticeException e){
			return R.fail(e.getR().getMsg());
		}
	}
	/**
	 * 远程调用分页接口
	@PostMapping("top")
	@ApiOperationSupport(order = 13)
	@ApiOperation(value = "分页远程调用", notes = "传入current,size")
	public R<BladePage<Notice>> top(@ApiParam(value = "当前页") @RequestParam Integer current, @ApiParam(value = "每页显示条数") @RequestParam Integer size) {
		BladePage<Notice> page = noticeClient.top(current, size);
		return R.data(page);
	}*/

}
